<?php
/**注意此文档为UTF-8的编码.
 * Created on 2010-6-16
 *
 * @author EXT <isrn@qq.com>
 *
 */
 class render{
 	private $name;
 	private $fieldLabel;
 	private $type;
 	private $config;
 	private $back = "";
 	private $disable = false;
	function set_name($name){
		$this->name = $name;
	}
	function set_fieldLabel($fieldLabel){
		$this->fieldLabel = $fieldLabel;
	}
	function set_type($type){
		$this->type = $type;
	}
	function set_config($config){
		$this->config = $config;
	}
	function get_js(){
		$config = json_decode($this->config,true);
		$this->config = $this->config?"disabled:'true',":"";
		switch($this->type){
				case 'datefield':
				$js ="{
						".$this->disable."
						xtype: 'datefield',
			            fieldLabel: '{$this->fieldLabel}',
			            name: '{$this->name}',
			            format:'{$config['format']}',
			            ".$this->back."
			            anchor:'30%'
		        	}";
				break;
				case 'textarea':
					$js ="{
						".$this->disable."
						xtype: 'textarea',
			            fieldLabel: '{$this->fieldLabel}',
			            name: '{$this->name}',
			            anchor:'98%',
			            ".$this->back."
			            height:{$config['height']}
		        	}";
				break;
				case 'htmleditor':
					$js="{
							".$this->disable."
						 	xtype:'htmleditor',
				            name:'{$this->name}',
				            fieldLabel:'{$this->fieldLabel}',
				            height:{$config['height']},
				            ".$this->back."
				            anchor:'98%'
					}";

				break;
				case 'fckeditor':
					$js="{
						".$this->disable."
						xtype:'textarea',
						name:'{$this->name}',
						value:'',
						anchor:'98%',
						height:100,
						fieldLabel:'{$this->fieldLabel}',
						".$this->back."
						listeners:{
						     focus:function(f){
						     		var myMask = new Ext.LoadMask(Ext.getBody(), {msg:'请等待'});
									myMask.show();
									var w=f.getSize();
						            fckEditor = new FCKeditor('{$this->name}') ;
						            fckEditor.Height='{$config['height']}';
						            fckEditor.Width=w.width;
						            fckEditor.BasePath = '/Plug-in/fck/' ;
						            fckEditor.ReplaceTextarea() ;
						            myMask.hide();
						                     }
						 }}
				";
				break;
				case 'swfupload':
					$js ="{
							".$this->disable."
					            xtype: 'fileuploadfield',
					            fieldLabel: '{$this->fieldLabel}',
					            ".$this->back."
					            anchor:'50%',
					            name: '{$this->name}',
					            buttonCfg: {
					                text: '',
					                iconCls: 'upload-icon'
					            }
					        }
					";
				break;
				case 'combo':
					$js ="{
							".$this->disable."
							typeAhead: true,
							name : '{$this->name}_',
							hiddenName : '{$this->name}',
							xtype: 'combo',
							anchor:'30%',
							fieldLabel : '{$this->fieldLabel}',
							store:new Ext.data.SimpleStore({
											fields:['name','value'],
											data:[{$config['pair']}]}),
							mode:'local',
					        displayField:'name',
					        ".$this->back."
					        selectOnFocus:true,
					        valueField :'value',
					        triggerAction:'all'
							}
					";
				break;
				case 'radiogroup':
					$config_num = count($config);
					$items = '';
					foreach($config as $id => $data){
						if($data['checked']=='1'){
							$checked = ", checked: true";
						}else
							$checked = "";

						$items.= "{boxLabel: '{$data['name']}', name: '{$this->name}', inputValue: {$data['value']}{$checked}}";
						if($id < $config_num-1){
							$items.= ",";
						}
					}
					$js ="{
								".$this->disable."
				                xtype: 'radiogroup',
				                fieldLabel: '{$this->fieldLabel}',
				                ".$this->back."
				                columns: 5,
				                anchor:'50%',
				                items: [
				                   {$items}
				                ]
				            }
					";
				break;
				case 'checkboxgroup':
					$config_num = count($config);
					$items = '';
					foreach($config as $id => $data){
						if($data['checked']=='1'){
							$checked = ", checked: true";
						}else
							$checked = "";
						$items.= "{boxLabel: '{$data['name']}', name: '{$data['value']}'{$checked}}";
						if($id < $config_num-1){
							$items.= ",";
						}
					}
					$js ="{
							".$this->disable."
				                xtype: 'checkboxgroup',
				                fieldLabel: '{$this->fieldLabel}',
				                ".$this->back."
				                columns: 5,
				                anchor:'50%',
				                items: [
				                   {$items}
				                ]
				            }
					";
				break;
				case 'password':
					$js ="{
						".$this->disable."
						xtype: 'textfield',
						inputType: 'password',
			            fieldLabel: '{$this->fieldLabel}',
			            ".$this->back."
			            name: '{$this->name}'
		        	}";
		        	if($config['confirm']){
		        		$js .=',';
		        		$js .="{
		        		".$this->disable."
				        fieldLabel: '确认{$this->fieldLabel}',
				        name: '{$this->name}-cfrm',
				        xtype: 'textfield',
				        inputType: 'password',
				        vtype: 'password',
				        ".$this->back."
				        initialPassField: '{$this->name}'
				      }";
		        	}
		        break;
			default:
					$js="{
						disabled:'true',
						xtype: '{$this->type}',
			            fieldLabel: '{$this->fieldLabel}',
			            name: '{$this->name}',
			            ".$this->back."
			            anchor:'60%'
		        	}";
		  }
		  return $js;
	}
		function get_plugjs(){
			switch($this->type){
				case 'datefield':
					$plugjs ='Date.dayNames = ["日", "一", "二", "三", "四", "五", "六"];
						    if (Ext.DatePicker) {
						        Ext.apply(Ext.DatePicker.prototype, {
						            todayText: "今天",
						            minText: "日期在最小日期之前",
						            maxText: "日期在最大日期之后",
						            disabledDaysText: "",
						            disabledDatesText: "",
						            monthNames: Date.monthNames,
						            dayNames: Date.dayNames,
						            nextText: "下月 (Control+Right)",
						            prevText: "上月 (Control+Left)",
						            monthYearText: "选择一个月 (Control+Up/Down 来改变年)",
						            todayTip: "{0} (Spacebar)",
						            okText: "确定",
						            cancelText: "取消",
						            format: "y年m月d日"
						        });
   					}';
				break;
			}

		}
 }
?>
